package com.central.scheduler.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.central.common.feign.ProjectService;
import com.central.common.model.PageResult;
import com.central.common.model.Result;
import com.central.common.model.SysProject;
import com.central.common.model.WorkerServer;
import com.central.common.service.impl.SuperServiceImpl;
import com.central.scheduler.api.service.IProWorkerService;
import com.central.scheduler.dao.mapper.WorkerGroupMapper;
import com.central.scheduler.dao.mapper.WorkerServerMapper;
import com.central.scheduler.dao.model.ProWorker;
import com.central.scheduler.api.service.IWorkerServerService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;


/**
 * @author Tindy
 * @date 2021/6/24
 * @describe
 */
@Slf4j
@Service
public class WorkerServerServiceImpl extends SuperServiceImpl<WorkerServerMapper, WorkerServer> implements IWorkerServerService {

    @Autowired
    private IProWorkerService proWorkerService;
    @Autowired
    private ProjectService projectService;
    @Autowired
    private WorkerGroupMapper workerGroupMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result add(String name, String description, String host) {
        //检查机器是否已存在
        if(baseMapper.selectCount(new QueryWrapper<WorkerServer>().eq("host",host))>0){
            return Result.failed("操作失败：机器host已存在");
        }
        WorkerServer workerServer = new WorkerServer();
        workerServer.setName(name);
        workerServer.setDescription(description);
        workerServer.setHost(host);
        workerServer.setStatus(0);
        workerServer.setResInfo("{}");
        baseMapper.insert(workerServer);
        return Result.succeed("操作成功");
    }

    @Override
    public PageResult<WorkerServer> findWorkers(Map<String, Object> params) {
        Page<WorkerServer> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<WorkerServer> list = baseMapper.findWorkers(page, params);
        long total = page.getTotal();
        return PageResult.<WorkerServer>builder().data(list).code(0).count(total).build();
    }

    @Override
    public PageResult<WorkerServer> findWorkersByPro(Map<String, Object> params, Long proId) {
        Page<WorkerServer> page = new Page<>(MapUtils.getInteger(params, "page"), MapUtils.getInteger(params, "limit"));
        List<WorkerServer> list = baseMapper.findWorkersByPro(page, params,proId);
        long total = page.getTotal();
        return PageResult.<WorkerServer>builder().data(list).code(0).count(total).build();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result setPros(Long workerId, Set<Long> proIds) {
        //删除旧项目
        proWorkerService.remove(new QueryWrapper<ProWorker>().eq("worker_id",workerId));
        ArrayList<ProWorker> proWorkers = new ArrayList<>();
        for (Long proId : proIds) {
            proWorkers.add(new ProWorker(proId,workerId));
        }
        proWorkerService.saveBatch(proWorkers);
        return Result.succeed("操作成功");
    }

    @Override
    public List<SysProject> getPros(Long workerId) {
        List<Long> proIds=proWorkerService.getProIds(workerId);
        //处理空报错
        proIds.add(-1l);
        return projectService.findProByProIds(proIds);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result removeByIdAndOff(Long workerId) {
        WorkerServer workerServer = baseMapper.selectById(workerId);
        if(workerServer.getStatus()==0){
            baseMapper.deleteById(workerId);
            return Result.succeed("操作成功");
        }else {
            return Result.succeed("操作失败，节点在线不可删除");
        }
    }
}
